package com.lottery.api.task;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lottery.api.service.EventsInfoService;
import com.lottery.common.contants.RedisConstants;
import com.lottery.common.entity.CompetitionRecord;
import com.lottery.common.service.CompetitionRecordService;
import com.lottery.common.service.SysConfigService;
import com.lottery.common.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.nutz.lang.util.NutMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


@Component
@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
@Slf4j
public class CompetitionTimedTask {


    @Autowired
    private CompetitionRecordService crs;

    @Autowired
    private SysConfigService scs;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private EventsInfoService eis;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //添加定时任务

    /**
     * 定时清理能量值
     */
//    @Scheduled(cron = "0 */1 * * * ?")
    private void initEnergyValue() {
        System.out.println("energyValue clean");
        log.info("energyValue clean start");
//        redisTemplate.delete(RedisConstants.ENERGY_VALUE);
        log.info("energyValue clean end");
    }


//    @Scheduled(cron = "0 */10 * * * ?")
    private void initCompetition() {
        System.out.println("init Competition");
//        crs.batchTest();

        log.info("init Competition start");
        String str= stringRedisTemplate.opsForValue().get("lastEventId");
        NutMap nutMap=new NutMap();
        nutMap.setv("type",1).setv("eventId",str);
        List<NutMap> list=eis.selectEvents(nutMap);
        log.info("list size is {}",list.size());
        if(list.size()<=0){
            return ;
        }
        disassembleMap(list);
        stringRedisTemplate.opsForValue().set("lastEventId",list.get(0).getString("比赛id"));
        crs.batchInsert(list);
//        List<NutMap> list2=new ArrayList<>();
//        list2.add(list.get(0));
//        list2.add(list.get(0));
        log.info("init Competition end");

    }

//    @Scheduled(cron = "0 */12 * * * ?")
    private void updateCompetition() {
        System.out.println(" update Competition");
        log.info(" update Competition start");
        String str= stringRedisTemplate.opsForValue().get("lastEventUpdateTime");
        NutMap nutMap=new NutMap();
        nutMap.setv("type",2).setv("updateTime",str);
        List<NutMap> list=eis.selectEvents(nutMap);
        log.info("list size is {}",list.size());
        if(list.size()<=0){
            return ;
        }
        disassembleMap(list);
        stringRedisTemplate.opsForValue().set("lastEventUpdateTime",list.get(0).getString("更新时间"));
        crs.batchUpdate(list);
//        List<NutMap> list2=new ArrayList<>();
//        list2.add(list.get(0));
//        list2.add(list.get(1));
//        crs.batchUpdate(list2);

        log.info(" update Competition end");
    }



    public void disassembleMap(List<NutMap> list){
        for(NutMap map:list){
            if (Integer.valueOf(map.getString("让球值").split(",")[0])>0){
                map.setv("让球",map.getString("让球值").split(",")[1])
                        .setv("让胜赔率",map.getString("胜率").split(",")[1])
                        .setv("让平赔率",map.getString("平率").split(",")[1])
                        .setv("让负赔率",map.getString("负率").split(",")[1]);
            }else{
                map.setv("让球",map.getString("让球值").split(",")[0])
                        .setv("让胜赔率",map.getString("胜率").split(",")[0])
                        .setv("让平赔率",map.getString("平率").split(",")[0])
                        .setv("让负赔率",map.getString("负率").split(",")[0])
                        .setv("让球数值",map.getString("让球值").split(",")[1])
                        .setv("胜赔率",map.getString("胜率").split(",")[1])
                        .setv("平赔率",map.getString("平率").split(",")[1])
                        .setv("负赔率",map.getString("负率").split(",")[1]);
            }
        }

    }
}
